﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Xml;


namespace GIVisualEditor
{
    [Serializable()]
    public class Node
    {
        private string name = "";
        private bool isLiteral = false;
        private bool isCaseSensitive = false;
        private bool allowStopHere = false;
        //private Position spatialInfo = new Position();
        private StateViewControl view = null;
        private DirectedGraph graph = null;
        private bool updateRequired = true;
        private bool isSelected = false;
        private bool isStartNode = false;

        public bool IsStartNode
        {
            get { return isStartNode; }
            set { isStartNode = value; }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; }
        }

        [Browsable(false)]
        public StateViewControl View
        {
            get { return view; }
        }
        
        [CategoryAttribute("State"), DescriptionAttribute("This name will be used to capture the value entered by user. Literal should not be named.")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [CategoryAttribute("State"), DescriptionAttribute("Pattern that input word must satisfy.")]
        public string Pattern
        {
            get { return view.Text; }
            set { view.Text = value; }
        }

        [CategoryAttribute("State"), DescriptionAttribute("If false then Pattern will be treated as Regular Expression.")]
        public bool IsLiteral
        {
            get { return isLiteral; }
            set { isLiteral = value; }
        }

        [CategoryAttribute("State"), DescriptionAttribute("Should the check be case sensitive.")]
        public bool IsCaseSensitive
        {
            get { return isCaseSensitive; }
            set { isCaseSensitive = value; }
        }

        [CategoryAttribute("State"), DescriptionAttribute("When checking a sentence, if flow stops here, should we consider it as acceptable.")]
        public bool AllowStopHere
        {
            get { return allowStopHere; }
            set { allowStopHere = value; }
        }

        [Browsable(false)]
        public int Left
        {
            get { return this.view.Left; }
            set { this.view.Left = value; }
        }

        [Browsable(false)]
        public int Top
        {
            get { return this.view.Top; }
            set { this.view.Top = value; }
        }

        //public void UpdateView()
        //{
        //    if (updateRequired)
        //    {
        //        this.view.Left = this.spatialInfo.Left;
        //        this.view.Top = this.spatialInfo.Top;
        //        this.view.Width = this.spatialInfo.Width;
        //        this.view.Height = this.spatialInfo.Height;
        //    }
        //}

        //internal void UpatePosition(int x, int y)
        //{
        //    updateRequired = false;
        //    this.spatialInfo.Left = x;
        //    this.spatialInfo.Top = y;

        //    updateRequired = true;
        //}

        public Node(DirectedGraph g)
        {
            this.graph = g;
            this.view = new StateViewControl(g, this);
            //this.spatialInfo.PositionChanged += new PropertyChangedHandler(spatialInfo_PositionChanged);
            
        }

        public void ToXML(ref XmlWriter oWriter, string id)
        {
            oWriter.WriteStartElement("Node");
            DirectedGraph.WriteNodeVale("Id", id, ref oWriter);
            DirectedGraph.WriteNodeVale("Name", this.name, ref oWriter);
            DirectedGraph.WriteNodeVale("Pattern", this.Pattern, ref oWriter);
            DirectedGraph.WriteNodeVale("IsLiteral", this.isLiteral.ToString().ToLower(), ref oWriter);
            DirectedGraph.WriteNodeVale("IsCaseSensitive", this.isCaseSensitive.ToString().ToLower(), ref oWriter);
            DirectedGraph.WriteNodeVale("IsAcceptable", this.allowStopHere.ToString().ToLower(), ref oWriter);
            DirectedGraph.WriteNodeVale("IsStartState", this.isStartNode.ToString().ToLower(), ref oWriter);

            // Put UI location information
            oWriter.WriteStartElement("UIPosition");
            DirectedGraph.WriteNodeVale("Left", this.view.Left.ToString(), ref oWriter);
            DirectedGraph.WriteNodeVale("Top", this.view.Top.ToString(), ref oWriter);

            oWriter.WriteEndElement();
            oWriter.WriteEndElement();

        }
    }
}
